package com.maxjay.component.metadata.model;

import com.maxjay.component.metadata.constant.FieldType;
import com.maxjay.component.metadata.core.formatter.FieldFormatter;
import com.maxjay.component.metadata.core.generator.FieldValueGenerator;

public class DefaultEntityField implements EntityField {
	
	private String id;
	
	private String name;
	
	private String nameCn;
	
	private boolean isKey = false;
	
	private boolean isDisplay = true;
	
	private boolean isNullable = true;
	
	private boolean isVirtual = false;
	
	private boolean isQueryField = false;
	
	private boolean isListField = false;
	
	private FieldType fieldType;
	
	private FieldFormatter formatter;
	
	private FieldValueGenerator fieldValueGenerator;
	
	public DefaultEntityField() {
	}
	
	public DefaultEntityField(String id, String name, String nameCn) {
		this.id = id;
		this.name = name;
		this.nameCn = nameCn;
	}
	
	public DefaultEntityField(String id, String name, String nameCn, FieldType fieldType) {
		this.id = id;
		this.name = name;
		this.nameCn = nameCn;
		this.fieldType = fieldType;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public String getNameCn() {
		return this.nameCn;
	}

	public FieldFormatter getFormatter() {
		return formatter;
	}

	public void setFormatter(FieldFormatter formatter) {
		this.formatter = formatter;
	}

	@Override
	public FieldType getFieldType() {
		return fieldType;
	}

	@Override
	public String convertDisplayValue(Record record) {
		if(formatter == null) {
			return (String)record.getValue(name);
		} else {
			return formatter.convertDisplayVal(this, record);
		}
	}

	@Override
	public Object convertStoreValue(Record record) {
		if(formatter == null) {
			return record.getValue(name);
		} else {
			return formatter.convertStoreVal(this, record);
		}
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public boolean isKey() {
		return isKey;
	}

	@Override
	public boolean isListField() {
		return isListField;
	}

	@Override
	public boolean isQueryField() {
		return isQueryField;
	}

	@Override
	public boolean isDisplay() {
		return isDisplay;
	}
	
	@Override
	public boolean isNullable() {
		return isNullable;
	}

	@Override
	public boolean isVirtual() {
		return isVirtual;
	}
	
	public void setNullable(boolean isNullable) {
		this.isNullable = isNullable;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNameCn(String nameCn) {
		this.nameCn = nameCn;
	}

	public void setKey(boolean isKey) {
		this.isKey = isKey;
	}

	public void setDisplay(boolean isDisplay) {
		this.isDisplay = isDisplay;
	}

	public void setVirtual(boolean isVirtual) {
		this.isVirtual = isVirtual;
	}

	public void setQueryField(boolean isQueryField) {
		this.isQueryField = isQueryField;
	}

	public void setListField(boolean isListField) {
		this.isListField = isListField;
	}

	public void setFieldType(FieldType fieldType) {
		this.fieldType = fieldType;
	}

	public void setValueGenerator(FieldValueGenerator fieldValueGenerator) {
		this.fieldValueGenerator = fieldValueGenerator;
	}

	@Override
	public FieldValueGenerator getValueGenerator() {
		return fieldValueGenerator;
	}
	
}
